<template>
	<section>
		<!--系统设置列表-->
		<el-table :data="systemSet" border v-loading="systemSetLoading" class="system-set-list">
			<el-table-column type="index" label="#" width="60">
			</el-table-column>
			<el-table-column prop="CategoryDesc" label="类型">
			</el-table-column>
			<el-table-column label="内容">
				<template scope="scope">
					<a style="cursor: pointer;color:#20a0ff;" @click="fnOpenUpdate(scope.row.ID,scope.row.ValueType)" :formatter="fnNullFormat(scope)">{{scope.row.Value}}</a>
				</template>
			</el-table-column>
			<el-table-column prop="Desc" label="描述">
			</el-table-column>
		</el-table>
		
		<!--编辑界面-->
		<el-dialog title="编辑" v-model="updateFormVisible" :close-on-click-modal="false">
			<el-form :model="updateForm" label-width="100px" ref="updateForm">
				<block v-show="updateType == 'color'">
					<el-form-item label="值">
						<el-color-picker v-model="updateForm.color"></el-color-picker>
					</el-form-item>
				</block>
				<block v-show="updateType == 'number'">
					<el-form-item label="值">
						<el-input-number v-model="updateForm.floor" :min="1" :max="99" label="请输入楼层"></el-input-number>
					</el-form-item>
				</block>
				<block v-show="updateType == 'string'">
					<el-form-item label="值">
						<el-input v-model="updateForm.msg" placeholder="请输入会议室冲突信息"></el-input>
					</el-form-item>
				</block>
				
				<el-form-item label="描述">
					<el-input type="textarea" :rows="2" placeholder="请输入描述" v-model="updateForm.desc"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="updateFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="fnUpdate">保存</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
    import Svc from '../../service/index.js'
	export default {
		data() {
			return {
				systemSetLoading : false,
				systemSet : [],
				currentID : 0,
				updateType : '',
				updateFormVisible : false,
				updateForm : {
					color : null,
					floor : 0,
					msg : '',
					desc : ''
				}
			}
		},
		methods: {
            //显示message提示框
            openMessage : function(msg,type){
    	        this.$message({
		          	showClose: true,
		          	message: msg,
		          	type: type
		        });
		    },
		    //格式化  
            fnNullFormat(scope) {
	          	if (scope.row.Value == "") { 
	          		scope.row.Value = "----"
	          	}  
            },
		    //获取所有系统设置
		    fnGetSystemSet(){
		    	var that = this
				that.systemSetLoading = true
	            Svc.fnGetData('/api/setting/all',{},function(res){
	            	if (res.body.ErrCode == 0){
	            		that.systemSet = res.body.Data
	            		that.systemSetLoading = false
	            	} else {
	            		that.openMessage("系统设置获取失败！",'error')
	            	}
	            },function(res){
	            	that.openMessage("网络错误，请重新！",'error')
	            })
		    },
		    //打开更新页面
		    fnOpenUpdate(id,valueType){
		    	var that = this
		    	that.updateType = valueType
		    	that.currentID = id
		    	that.updateFormVisible = true
		    	that.fnGetCurrent()
		    },
		    //获取信息
	      	fnGetCurrent(){
	      		var that = this
	            Svc.fnGetData('/api/setting/detail/' + that.currentID,{},function(res){
	            	if (res.body.ErrCode == 0){
	            		if (res.body.ErrCode == 0) {
	            			if (that.updateType == 'color') {
	            				that.updateForm.color = res.body.Data.Value
	            			} else if (that.updateType == 'number') {
	            				that.updateForm.floor = res.body.Data.Value
	            			} else if (that.updateType == 'string') {
	            				that.updateForm.msg = res.body.Data.Value
	            			}
	            			
	            			that.updateForm.desc = res.body.Data.Desc
	            		}
	            	} else {
	            		that.openMessage("获取领导信息失败！" + res.body.ErrMsg,'error')
	            	}
	            },function(res){
	            	that.openMessage("网络错误，请重新！",'error')
	            })
	      	},
		    //编辑系统设置
	      	fnUpdate(){
	      		var that = this
		    	var params = {
					ID : that.currentID,
        			Value : '', 
					Desc : that.updateForm.desc
		    	}
		    	
		    	if (that.updateType == 'color') {
    				params.Value = that.updateForm.color
    			} else if (that.updateType == 'number') {
    				params.Value = that.updateForm.floor.toString()
    			} else if (that.updateType == 'string') {
    				params.Value = that.updateForm.msg
    			}
		    	
	            Svc.fnPostData('/api/setting/modify',params,function(res){
	            	if (res.body.ErrCode == 0){
	            		that.updateFormVisible = false
	            		that.fnGetSystemSet()
	            	} else {
	            		that.openMessage("更新失败失败！" + res.body.ErrMsg,'error')
	            	}
	            },function(res){
	            	that.openMessage("网络错误，请重新！",'error')
	            })
		    }
		},
		mounted() {
			//获取所有系统设置
		    this.fnGetSystemSet()
		}
	}

</script>

<style scoped>
	.system-set-list{
		width: 100%;
		margin-top: 20px;
	}
</style>